#!/usr/bin/env python
# encoding: utf-8
'''
@author: Excelsiorly
@license: (C) Copyright 2021, All Rights Reserved.
@contact: excelsiorly@qq.com
@file: 0034.py
@time: 2021/11/18 15:16
@desc: https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/
'''

class Solution(object):
    def lastRemaining(self, n, m):
        """
        :type n: int
        :type m: int
        :rtype: int
        """
        lis = list(range(n))
        i = 0
        while len(lis) > 1:
            i = lis.pop((i+m)%len(lis)-1) - 1

        return lis[0]

        return lis[0]

if __name__ == '__main__':
    res = Solution().lastRemaining(n=5, m = 2)
    print(res)